<?php defined("MAIN_DIR") or die("No direct access");

$poll_id = UV::readInt($route->getPath(3));
if (!$poll_id) {
	to("/$c_name/polls/");
}

$poll_res = mysql_query("select * from `poll` where `poll_id` = '$poll_id' limit 1");
if (mysql_num_rows($poll_res) == 0) {
	to("/$c_name/polls/");
}

$poll_row = mysql_fetch_array($poll_res);

$h1 = $poll_row['text'];
$view_results = false; // спросить или показать результаты
$ua_res = mysql_query("select * from `user_answers` where `poll_id` = '$poll_id' and `user_ip` = '$_SERVER[REMOTE_ADDR]'");
if (mysql_num_rows($ua_res) == 0) {
	if (isset($_GET['answer']) && $_GET['answer'] != "") {
		if (mysql_num_rows(mysql_query("select * from `answers` where `poll_id` = '$poll_id' and `answer_id` = '$_GET[answer]' limit 1")) > 0) {
			mysql_query("insert into `user_answers` (`poll_id`,`answer_id`,`user_ip`) values ('$poll_id','$_GET[answer]','$_SERVER[REMOTE_ADDR]')");
			$view_results = true;
			$h1 = "Результаты опроса";
		}
	}
} else {
	$view_results = true;
	$h1 = "Результаты опроса";
}

$answers_res = mysql_query("select `A`.*,COUNT(`UA`.`ua_id`) as `count` from `answers` as `A` left join `user_answers` as `UA` on `A`.`answer_id` = `UA`.`answer_id` where `A`.`poll_id` = '$poll_id' group by `A`.`answer_id` order by `answer_id`");
if ($view_results) {
	$answers = array();
	while ($answers_row = mysql_fetch_array($answers_res)) {
		$answers[] = $answers_row['count'];
	}

	$answers_percent = array();
	$hundred = 0;
	for($i=0; $i<count($answers); $i++) {
		$hundred += $answers[$i];
	}
	for($i=0; $i<count($answers); $i++) {
		$num1 = $answers[$i]/$hundred * 100;
		$num2 = number_format($answers[$i] / $hundred * 100,0);
		if ($num1 == $num2) {
			$answers_percent[] = $num1 = $answers[$i]/$hundred * 100;
		} else {
			$answers_percent[] = number_format($answers[$i] / $hundred * 100,1);
		}
	}

	$max = 0;
	for($i=1; $i<count($answers); $i++) {
		if ($answers[$max] < $answers[$i]) {
			$max = $i;
		}
	}
}

require_once APP.HEADFILE;
?>
<script type="text/javascript" src="/files/js/bank_list.js"></script>
<h1><?php echo $h1; ?></h1>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td align="left" valign="top" class="banki_main_col">
			<strong>Вопрос</strong>: <?php echo $poll_row['text']; ?>
			<p><strong>Ответы</strong>:</p>
			<div id="opros_table">
				<table width="100%" border="0" cellpadding="8" cellspacing="0"><?php
					mysql_data_seek($answers_res,0);
					$i = 0;
					if ($view_results)
					{
						while($answers_row = mysql_fetch_array($answers_res))
						{
							$i++;
							?><tr>
								<td width="74%"><?php echo $i.". ".$answers_row['caption']; ?></td>
								<td width="26%">
									<strong <?php
									if ($max == $i-1)
										echo "class='oprosy_winner'";
									?>><?php echo $answers_percent[$i-1];?> </strong>
									<span class="opros_small_grey">%</span>
								</td>
							</tr><?php
						}
					}
					else
					{
						while ($answers_row = mysql_fetch_array($answers_res))
						{
							$i++;
							?><tr>
								<td><?php
									echo $i.". <a href='/$c_name/polls/$poll_row[poll_id]?answer=$answers_row[answer_id]'>";
									echo $answers_row['caption']."</a>";
								?></td>
							</tr><?php
						}
						?><tr><td></td></tr><?php
					}
				?></table><?php
				if ($view_results)
				{
					?><div class="oprosy_image">
						<img src="/<?php echo $c_name; ?>/graph/<?php echo $poll_id; ?>" />
					</div><?php
				}
				?><div>&larr; <a href="<?php echo "/$c_name";?>/polls/">Вернуться к списку опросов</a></div>
			</div>
		</td>
		<td width="218" align="left" valign="top"><?php
			require_once APP."rating_column.php";
			require_once APP."vacancy_column.php";
		?></td>
	</tr>
</table>
<div class="inside_block">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr valign='top'>
			<td width="344" class="inside_info"><?php
				require_once APP."bank_list.php";
				require_once APP."currency_rates.php";
			?></td>
			<td width="646"><?php
				$info = mysql_fetch_array(mysql_query("select * from `info` where `info_id` = '7' limit 1"));
				$text = getRichText($info['text']);
				echo "<h2>$info[header]</h2>";
				echo "<p>$text</p>";
			?></td>
		</tr>
	</table>
</div>
<?php require_once APP.FOOTERFILE; ?>